package com.glorysoft.sunnypda.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import androidx.annotation.RequiresApi;

import com.glorysoft.sunnypda.base.MyApp;
import com.glorysoft.sunnypda.ui.login.LoginActivity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;

import java.util.Locale;

/**
 * FileName: ChangeLanguageHelper
 * Author: XJH
 * Date: 2022/1/10 17:01
 * Description:
 * History:
 */
public class ChangeLanguageHelper {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void init(Context context){
        Resources resources = context.getResources();
        String country = context.getResources().getConfiguration().locale.getCountry();
    }

    /**
     * 更改app语言
     * @param context
     * @param locale
     * @param language
     */
    public static void changeAppLanguage(Context context, Locale locale, String language) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        resources.updateConfiguration(configuration, metrics);
        if (!language.isEmpty()){
            FreeApi_SharePreferencesUtils.setSharePre("SUNNY_FILE", "LANGUAGE", language, MyApp.app);
        }


    }

    //重启app
    public static void restart(Context context){
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

    public static Locale getSystemLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言
            locale = Resources.getSystem().getConfiguration().getLocales().get(0);
        } else {
            locale = Resources.getSystem().getConfiguration().locale;
        }
        return locale;
    }
}
